﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using CapstoneWinPhone.Data;
using System.Collections.ObjectModel;
using System.Linq;

namespace CapstoneWinPhone.Data
{
    public class WeeklyTimeStamps
    {
        public ObservableCollection<Timestamps> listOfTimestamps { get; set; }
        public int _WeekNumber { get; set; }
        public int _Year { get; set; }
        public double totalHours { get; private set; }
        public double totalMinutes { get; private set; }
        public string totalTime
        {
            get
            {
                return TotalHoursAndMinutes();
            }
        }
        public WeeklyTimeStamps()
        {

        }

        public WeeklyTimeStamps(int weekNumber)
        {
            _WeekNumber = weekNumber;
            _Year = DateTime.Now.Year;
            listOfTimestamps = new ObservableCollection<Timestamps>();
        }

        public void CreateFinishTimestamp()
        {
            Timestamps t = GetCurrentTimeStamp();
            if (t != null)
            {
                t.EndTime = DateTime.Now;
            }
            else
            {
                t = new Timestamps(DateTime.Now);
                listOfTimestamps.Add(t);
            }
        }

        public Timestamps GetCurrentTimeStamp()
        {
            if (listOfTimestamps.Count > 0)
            {
                try
                {
                    return listOfTimestamps.Last(ts => !ts.IsFinishedTimeStamp);
                }
                catch (InvalidOperationException)
                {
                }
            }
            return null;
        }

        public bool ClockedIn()
        {
            return listOfTimestamps.Where(t => t.IsFinishedTimeStamp == false).FirstOrDefault() != null ? true : false;
        }

        public string TotalHoursAndMinutes()
        {
            totalHours = 0;
            totalMinutes = 0;
            foreach (Timestamps ts in listOfTimestamps)
            {
                if (ts.IsFinishedTimeStamp)
                {
                    totalHours += DataMethods.CalculateHours(ts.StartTime, ts.EndTime);
                    totalMinutes += DataMethods.CalculateMinutes(ts.StartTime, ts.EndTime);
                    if (totalMinutes >= 60)
                    {
                        totalHours++;
                        totalMinutes = totalMinutes - 60;
                    }
                }
            }
            return (int)totalHours + " hours and " + (int)totalMinutes + " minutes";
        }

        public override string ToString()
        {
            return _WeekNumber + weekString() + " week of " + _Year;
        }

        private string weekString()
        {
            if (_WeekNumber == 1)
            {
                return "st";
            }else if(_WeekNumber == 2){
                return "nd";
            }else{
                return "th";
            }
        }
    }
}
